package com.car.util;

import java.io.IOException;
import java.lang.reflect.Method;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 
 * BaseServlet：核心控制器
 * 类似SpringMVC的DispatcherServlet：
 * 1.接收请求
 * 2.分发请求到相应的业务控制器处理
 * @author 源哥
 *
 */
public class BaseServlet extends HttpServlet{
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
<<<<<<< HEAD
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
=======
>>>>>>> branch 'master' of https://gitee.com/kangdg/car.git

		String uri = request.getRequestURI();

		StringBuffer url = request.getRequestURL();

		System.out.println("uri = " + uri);
		System.out.println("url = " + url);
//		2.截取URI获取methodName
		String methodName = uri.substring(uri.lastIndexOf("/") + 1);
		System.out.println(methodName);
		
//		3.TODO:反射调方法
//		3.1 获取类对象
//		this:指代的是业务控制器对象
		Class<? extends BaseServlet> cls = this.getClass();
		System.out.println(cls.getName());
//		3.2 反射调用方法
		try {
			Method method = cls.getDeclaredMethod(methodName,
					HttpServletRequest.class,HttpServletResponse.class);
			method.invoke(this, request,response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}

}
